/**
 * Created by Administrator on 2016/5/25.
 */
var ip, port,ws,chatToken,userId,userName;
function chat(rid) {
    $.ajax({
        type: 'POST',
        url: "/Online/getToken",
        data: {'rid': rid},
        success: function (res) {
            var wsl = "ws://"+res.ip+":"+res.port+"";
            ws = new ReconnectingWebSocket(wsl);
            ws.onopen = function (event) {
                if(res.type == "connect"){
                    var data = "{\"type\" : \""+res.type+"\" ,\"roomId\" : \""+rid+"\", \"connectToken\":\""+res.token+"\"}";

                } else if(res.type == "login"){
                    var data = "{\"type\" : \""+res.type+"\" ,\"roomId\" : \""+rid+"\",\"userId\" : \""+res.userId+"\",\"userName\" : \""+res.userName+"\" ,\"loginToken\":\""+res.token+"\"}";
                    userId = res.userId;
                    userName = res.userName;
                }
                ws.send(data);

            };
            ws.onmessage = function (event) {
                var data = event.data;
                var data = eval('(' + data + ')');
                switch (data.type) {
                    //连接成功
                    case "connect":
                        console.log('游客连接成功！')
                        break;
                    //心跳开始检测
                    case "tick":
                        console.log("心跳检测..........");
                        break;
                    //心跳开始检测
                    case "login":
                        console.log("登录成功..........");
                        chatToken = data.chatToken;
                        break;
                    case "chat":
                        console.log("聊天消息:",data);
                        insertHtml(data.userName+":", data.message.message);
                }
                console.log(data);
            }
            ws.onclose = function (event) {
                //清除计时器
               // ws.refresh();
                console.log("连接关闭...................");
            };
        }
    })
}

function send() {
    var message = $.trim($("#inputMessage").val());
    var messageLength = message.length;
    console.log(message);
    if (userId != null) {
        if (message != "") {
            if (messageLength > 140) {
                alert("消息太长了，请分开发送！");
            }else{
                var data = "{\"type\":\"chat\",\"message\":{\"message\":\""+message+"\"},\"roomId\" : \""+rid+"\",\"userId\" : \""+userId+"\",\"userName\" : \""+userName+"\" ,\"chatToken\":\""+chatToken+"\"}";
                ws.send(data);
                $("#inputMessage").val("");
            }

        } else {
            alert("消息不可为空");
        }
    } else {
        $("#inputMessage").attr('disabled',"disabled");
        alert("请先登录！");

    }

}

function insertHtml(name, content) {
    var html = "<li class=\"chat_li middle\"><span class=\"chat_t3\">"+name+"</span> <span class=\"chat_t2\">"+content+"</span></li>";
    $("#chatroom").append(html);
    $(".chat").scrollTop($("#chatroom").height() - $(".chat").height() + 80);
    console.log($(".chat").height());
}

$(document).keypress(function (e) {
    // 回车键事件
    if (e.which == 13) {
        send();
    }
});